/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.topo.blinking;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import demo.DemoPane;

import twaver.AlarmSeverity;
import twaver.Element;
import twaver.Link;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.BlinkingRule;
import twaver.Node;
import twaver.network.TNetwork;
import twaver.network.background.ColorBackground;

public class BlinkingDemo extends DemoPane {
	
	class Rule1 extends BlinkingRule{
		public boolean isBodyBlinking(Element element) {
			return false;
		}
		public boolean isOutlineBlinking(Element element) {
			// blink when element has propagate severity
			return element.getAlarmState().getPropagateSeverity() != null;
		}		
	}
	
	class Rule2 extends BlinkingRule{
		public boolean isBodyBlinking(Element element) {
			// blink when element has new alarm
			return element.getAlarmState().getHighestNewAlarmSeverity() != null;
		}
		public boolean isOutlineBlinking(Element element) {
			// blink when element has state outline color
			return element.getStateOutlineColor() != null;
		}		
	}
	
	class Rule3 extends BlinkingRule{
		public boolean isBodyBlinking(Element element) {
			// if element has renderer color
			if(element.getRenderColor() != null){
				return true;
			}
			// blink when element's new alarm severity 
			// is more urgent than warning level
			AlarmSeverity severity = element.getAlarmState().getHighestNewAlarmSeverity();
			if(severity == AlarmSeverity.CRITICAL){
		        return true;
			}
			return false;
		}
		public boolean isOutlineBlinking(Element element) {
			return false;
		}
	}
	
    TDataBox box = new TDataBox();
    TNetwork network1 = new TNetwork(box);
    TNetwork network2 = new TNetwork(box);
    TNetwork network3 = new TNetwork(box);
    Rule1 ruler1 = new Rule1();
    Rule2 ruler2 = new Rule2();
    Rule3 ruler3 = new Rule3();
    
    public BlinkingDemo() {
    	ColorBackground background = new ColorBackground();
    	background.setGradient(true);
    	background.setColor(Color.GREEN.darker());
    	background.setGradientColor(Color.BLACK.brighter());
    	background.setGradientFactory(TWaverConst.GRADIENT_EXTEND_E);
    	box.setBackground(background);
    	
    	network1.setBlinkingRule(ruler1);
    	network2.setBlinkingRule(ruler2);
    	network3.setBlinkingRule(ruler3);
    	
    	network1.setToolbar(null);
    	network2.setToolbar(null);
    	network3.setToolbar(null);

    	JPanel panel = new JPanel(new GridLayout(3, 1));
    	panel.add(network1);
    	panel.add(network2);
    	panel.add(network3);
    	this.add(panel, BorderLayout.CENTER);
    	this.add(createControlPane(), BorderLayout.SOUTH);
        
        Iterator it = AlarmSeverity.iterator();
        int x = 30;
        Node node = null;
        int i = 0;
        while(it.hasNext()){
        	i++;
        	Node lastNode = node;
        	// add node
        	AlarmSeverity severity = (AlarmSeverity)it.next();
        	node = new Node();
        	node.setName(severity.getName());
        	node.putLabelColor(Color.WHITE);
        	node.setLocation(x, 70);
        	node.getAlarmState().addNewAlarm(severity);
        	if(AlarmSeverity.MINOR == severity){
            	node.getAlarmState().setPropagateSeverity(AlarmSeverity.WARNING);
        	}
        	box.addElement(node);
        	
        	// add link
        	if(lastNode != null){
        		Link link = new Link(lastNode, node);
        		link.getAlarmState().addNewAlarm(severity);
        		box.addElement(link);
        	}
        	lastNode = node;
        	x += 100;
        }

        node.setImage("/demo/resource/images/server2_image.png");
        node.putRenderColor(Color.BLUE);
        node.putStateOutlineColor(Color.GREEN);
        node.putDrawImageShape(true);
    }

    private JPanel createControlPane(){
    	JPanel panel = new JPanel();
    	panel.setLayout(new GridLayout(1, 4));
    	
    	final JCheckBox checkBox = new JCheckBox("enable blinking", true);
    	checkBox.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				ruler1.setEnableBlinking(checkBox.isSelected());
				ruler2.setEnableBlinking(checkBox.isSelected());
				ruler3.setEnableBlinking(checkBox.isSelected());
			}
    	});
    	ButtonGroup group = new ButtonGroup();
    	panel.add(checkBox);
    	panel.add(createJRadioButton(group, 200));
    	panel.add(createJRadioButton(group, 500));
    	panel.add(createJRadioButton(group, 1000));
    	return panel;
    }
    
    private JRadioButton createJRadioButton(ButtonGroup group, final int interval){
    	JRadioButton radioButton = new JRadioButton("" + interval);  	
    	group.add(radioButton);
    	radioButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				TWaverUtil.setAnimateBlinkInterval(interval);
			}
    	});
    	return radioButton;
    }

}